package in.muton.edushare.academics.panels;

import in.muton.edushare.academics.panels.GradePanelUtil.GradeKeyHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Universities;

public class UnivGradeKeyView extends EdusharePanel {
	private static final long serialVersionUID = 7744285931387350177L;
	private UnivGradeKeyPanel parentPanel;
	private IModel<Universities> univModel;

	public UnivGradeKeyView(String id, IModel<Universities> model,
			EduShareBasePage page, UnivGradeKeyPanel parentPanel) {
		super(id, model, page);
		this.parentPanel = parentPanel;
		this.univModel = model;
		add(new Form<Void>("form") {
			@Override
			public void onSubmit() {
				UnivGradeKeyView.this.parentPanel
						.setActivePanel(UnivGradeKeyPanel.UNIV_KEY_EDIT);
			}

			private static final long serialVersionUID = 3666475921918944761L;
		});

		
		add(new ListView<GradeKeyHelper>("gradeList", new IModel<List<GradeKeyHelper>>(){
			private static final long serialVersionUID = -2284828955642818905L;
			public List<GradeKeyHelper> getObject() {
				List<GradeKeyHelper> tupleList = GradePanelUtil.getTuples(univModel.getObject());
				Collections.sort(tupleList);
				Collections.reverse(tupleList);
				return tupleList;
			}

			public void setObject(List<GradeKeyHelper> arg0) {
			}

			public void detach() {
			}}
		) {
			@Override
			protected void populateItem(ListItem<GradeKeyHelper> arg0) {
				GradeKeyHelper t = arg0.getModelObject();
				arg0.add(new Label("gradeLetter", t.gradeName));
				arg0.add(new Label("minMarks", t.minMarks+""));
				arg0.add(new Label("maxMarks", t.maxMarks+""));
				arg0.add(new Label("gpaValue", t.gpaValue+""));
				arg0.add(new Label("failGrade", t.passGrade?"":"F"));
			}
			private static final long serialVersionUID = -280289763952425868L;
		});
	}

	@Override
	public boolean isVisible() {
		return parentPanel.getActivePanel() == UnivGradeKeyPanel.UNIV_KEY_VIEW;
	}
}
